package app.android.alampang.ui.activity;

import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;

import app.android.alampang.R;
import app.android.alampang.loader.FontLoader;
import app.android.alampang.loader.PlaceInfoLoader;
import app.android.alampang.model.Place;
import app.android.alampang.model.PlaceGroup;
import app.android.alampang.ui.adapter.PlaceListAdapter;
import app.android.alampang.ui.core.BaseActivity;
import app.android.alampang.ui.core.OnSwipeTouchListener;
import app.android.alampang.ui.core.SwipeHandler;
import app.android.alampang.utility.NavigationService;

public class CommunityActivity extends BaseActivity implements OnSwipeTouchListener, AdapterView.OnItemClickListener {

    private ListView listPlace;
    private PlaceListAdapter listPlaceAdapter;

    private SwipeHandler swipeHandler;
    private ArrayList<ImageView> imgList;

    private ImageView imgWall, imgStar, imgHome, imgMarket;
    private View layoutSwipe;
    private TextView txtName;

    private int currentPosition;
    private int xCenterOfDevice;
    private int normalImageSize = 300;
    private int selectedImageSize = 400;

    private ArrayList<PlaceGroup> groups;

    private String[] placeTypesName;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_community, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreate(Bundle savedInstanceState, Bundle param) {
        setContentView(R.layout.activity_community);

        imgList = new ArrayList<>();
        swipeHandler = new SwipeHandler(this);
        swipeHandler.setOnSwipeTouchListener(this);
        groups = PlaceInfoLoader.getAllPlaceGroups();

        placeTypesName = getResources().getStringArray(R.array.place_types);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        xCenterOfDevice = (size.x / 2);

        normalImageSize = (size.y /4);
        selectedImageSize = normalImageSize + ((normalImageSize / 100) * 30);
    }

    @Override
    public void initialize() {
        layoutSwipe =  findViewById(R.id.layout_swipe);

        imgWall = (ImageView) findViewById(R.id.img_wall);
        imgStar = (ImageView) findViewById(R.id.img_star);
        imgHome = (ImageView) findViewById(R.id.img_home);
        imgMarket = (ImageView) findViewById(R.id.img_market);

        imgList.add(imgWall);
        imgList.add(imgStar);
        imgList.add(imgHome);
        imgList.add(imgMarket);

        swipeHandler.attachOnTouchListener(layoutSwipe);

        listPlace = (ListView) this.findViewById(R.id.list_place);
        listPlaceAdapter = new PlaceListAdapter(this);
        listPlace.setOnItemClickListener(this);

        txtName = (TextView) this.findViewById(R.id.txt_name);
        txtName.setTypeface(FontLoader.getFont(this, FontLoader.TH_KRUB), Typeface.BOLD);

        setCurrentPosition(1);
    }

    @Override
    public void onSwipeMotion(View v, MotionEvent event) {

    }

    @Override
    public void onSwipeRight() {
        if (currentPosition - 1 >= 0)
        {
            --currentPosition;
        }
        setCurrentPosition(currentPosition);
    }

    @Override
    public void onSwipeLeft() {
        if (currentPosition + 1 < imgList.size())
        {
            ++currentPosition;
        }
        setCurrentPosition(currentPosition);
    }

    @Override
    public void onSwipeTop() {

    }

    @Override
    public void onSwipeBottom() {

    }

    @Override
    public void onLock() {

    }

    @Override
    public void onUnLock() {

    }

    @Override
    public void onReset() {
        for (ImageView i : imgList)
        {
            ViewGroup.LayoutParams layoutParams = i.getLayoutParams();
            layoutParams.width = normalImageSize;
            layoutParams.height = normalImageSize;
            i.setLayoutParams(layoutParams);
        }
    }

    private void setCurrentPosition(int position)
    {
        currentPosition = position;

        txtName.setText(placeTypesName[currentPosition]);

        onReset();

        ImageView curImg = imgList.get(currentPosition);

        ViewGroup.LayoutParams layoutParams = curImg.getLayoutParams();
        layoutParams.width = selectedImageSize;
        layoutParams.height = selectedImageSize;

        curImg.setLayoutParams(layoutParams);

        int xCenterOfCurrent = curImg.getLayoutParams().width / 2;
        curImg.setX(xCenterOfDevice - xCenterOfCurrent);

        for (int i = currentPosition + 1; i < imgList.size(); i++)
        {
            ImageView nextTxt = imgList.get(i);
            ImageView prevTxt = imgList.get(i-1);
            int pX =  (int)prevTxt.getX();
            int pW = prevTxt.getLayoutParams().width;


            nextTxt.setX((pX + pW) - 10);
        }

        for (int i = currentPosition - 1; i >= 0; i--)
        {
            ImageView nextTxt = imgList.get(i);
            ImageView prevTxt = imgList.get(i+1);

            int pX =  (int)prevTxt.getX();
            int pW = nextTxt.getLayoutParams().width;

            nextTxt.setX((pX - pW) + 10) ;
        }

        PlaceGroup curGroup = groups.get(currentPosition);
        listPlaceAdapter.setItems(curGroup.getPlaces());
        listPlace.setAdapter(listPlaceAdapter);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle param = new Bundle();
        //Toast.makeText(MenuPlaceActivity.this, listPlaceAdapter.get(position).toJSONObject().toString(),Toast.LENGTH_LONG).show();
        param.putString(Place.PARSING_NAME, listPlaceAdapter.get(position).toJSONObject().toString());

        NavigationService.getInstance().navigate(this, PlaceDetailActivity.class,
                param, false);
    }
}
